package com.lepu.app.fun.chat.adapter;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.utils.Const;
import com.app.utils.HeadIconLoadUtil;
import com.app.utils.LoginUtil;
import com.app.utils.SdLocal;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.BitmapManager;
import com.core.lib.core.HttpRequest;
import com.core.lib.utils.CallBack;
import com.core.lib.utils.main.ImageUtilBase;
import com.core.lib.utils.main.LogUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.photo.LocalImageLoader;
import com.core.lib.utils.photo.RotateImageViewAware;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.utils.secure.EncodeMD5;
import com.core.lib.widget.CircularImageView;
import com.core.lib.widget.MyDialog;
import com.lepu.app.fun.chat.bean.BeanLectureChatUser;
import com.lepu.app.fun.chat.bean.BeanStaffEntity;
import com.lepu.app.fun.chat.bean.ChatMessage;
import com.lepu.app.fun.chat.bean.ChatRoomMessage;
import com.lepu.app.fun.chat.bean.Media;
import com.lepu.app.fun.chat.util.ChatConst;
import com.lepu.app.fun.chat.util.ChatSendListener;
import com.lepu.app.fun.chat.util.ChatUtil;
import com.lepu.app.fun.chat.util.Constants;
import com.lepu.app.fun.chat.util.DateUtil;
import com.lepu.app.fun.chat.util.LectureUtil;
import com.lepu.app.fun.chat.util.ManagerClipborad;
import com.lepu.app.fun.chat.util.VoicePlayClickListener;
import com.lepu.app.main.activity.AppImageBroswerActivity;
import com.lepu.pasm.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class ChatMessageAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private List<ChatMessage> mArrayList = null;
	private int mType = ChatConst.TYPE_ONLINE_CHAT_ONLINE;
	private ChatSendListener mChatSendListener;

	private LinkedList<String> mImageDataContainer = new LinkedList<String>();
	ListView mListView;
	Bitmap mLoadFailBitmap = null;

	public ChatMessageAdapter(Context context,
			ChatSendListener chatSendListener, List<ChatMessage> arrayList,
			int type,ListView listView) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mType = type;
		mChatSendListener = chatSendListener;
		mListView = listView;
		mLoadFailBitmap = ImageUtilBase.getBitmapByResId(mContext,R.drawable.app_chat_image_download_error);

		// 初始化图片数组
		// 初始化图片数组
		if (mArrayList != null && mArrayList.size() > 0) {
			for (int i = 0;i<mArrayList.size();i++) {
				addImageData(mArrayList.get(i),true);
			}
		}
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	public void addImageData(ChatMessage message,boolean isLast)
	{
		try {
			if (message.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE) {

				String url = "";
				if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE)
				{
					url = message.getMedia().getLargeImageId();
				}else{
					url = message.getMedia().getFullName();
				}

				if(TextUtils.isEmpty(url))
				{
					url = message.getMedia().getFullName();
				}

				if(isLast)
				{
					mImageDataContainer.addLast(url);
				}else{
					mImageDataContainer.addFirst(url);
				}
			}
		}catch (Exception e){

		}
	}

	public void removeImageData(ChatMessage message)
	{
		try {
			if (message.getMessageType() == Constants.Chat.CHAT_MESSAGE_TYPE_IMAGE) {

				String url = "";
				if (message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE)
				{
					url = message.getMedia().getLargeImageId();
				}else{
					url = message.getMedia().getFullName();
				}

				mImageDataContainer.remove(url);
			}
		}catch (Exception e){
		}
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.chat_message_listitem, null);

			holder = new ViewHolder();

			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);
			// receive
			RelativeLayout receiveMessageLayout = (RelativeLayout) convertView
					.findViewById(R.id.receiveMessageLayout);
			CircularImageView receiveHeadIconImageView = (CircularImageView) convertView
					.findViewById(R.id.receiveHeadIconImageView);
			TextView receiveNameTextView = (TextView) convertView
					.findViewById(R.id.receiveNameTextView);// ?????
			LinearLayout receiveContentLayout = (LinearLayout) convertView
					.findViewById(R.id.receiveContentLayout);
			TextView receiveContentTextView = (TextView) convertView
					.findViewById(R.id.receiveContentTextView);
			ImageView receiveContentImageView = (ImageView) convertView
					.findViewById(R.id.receiveContentImageView);
			LinearLayout receiveContentYuYinLayout = (LinearLayout) convertView
					.findViewById(R.id.receiveContentYuYinLayout);

			TextView receiveContentYuYinTimeTextView = (TextView) convertView
					.findViewById(R.id.receiveContentYuYinTimeTextView);
			ImageView receiveChatVoiceImageView = (ImageView) convertView
					.findViewById(R.id.receiveChatVoiceImageView);

			LinearLayout receiveYuYinExtraLayout = (LinearLayout) convertView
					.findViewById(R.id.receiveYuYinExtraLayout);
			ImageView receiveYuyinReadImageView = (ImageView) convertView
					.findViewById(R.id.receiveYuyinReadImageView);

			ProgressBar receiveContentProgressBar = (ProgressBar) convertView
					.findViewById(R.id.receiveContentProgressBar);
			ImageView receiveContentTipImageView = (ImageView) convertView
					.findViewById(R.id.receiveContentTipImageView);

			// send
			RelativeLayout sendMessageLayout = (RelativeLayout) convertView
					.findViewById(R.id.sendMessageLayout);
			CircularImageView sendHeadIconImageView = (CircularImageView) convertView
					.findViewById(R.id.sendHeadIconImageView);
			TextView sendNameTextView = (TextView) convertView
					.findViewById(R.id.sendNameTextView);// ?????
			LinearLayout sendContentLayout = (LinearLayout) convertView
					.findViewById(R.id.sendContentLayout);
			TextView sendContentTextView = (TextView) convertView
					.findViewById(R.id.sendContentTextView);
			ImageView sendContentImageView = (ImageView) convertView
					.findViewById(R.id.sendContentImageView);
			LinearLayout sendContentYuYinLayout = (LinearLayout) convertView
					.findViewById(R.id.sendContentYuYinLayout);
			TextView sendContentYuYinTimeTextView = (TextView) convertView
					.findViewById(R.id.sendContentYuYinTimeTextView);
			ImageView sendChatVoiceImageView = (ImageView) convertView
					.findViewById(R.id.sendChatVoiceImageView);
			//
			ProgressBar sendContentProgressBar = (ProgressBar) convertView
					.findViewById(R.id.sendContentProgressBar);
			ImageView sendContentReImageView = (ImageView) convertView
					.findViewById(R.id.sendContentReImageView);

			LinearLayout sendYuYinExtraLayout = (LinearLayout) convertView
					.findViewById(R.id.sendYuYinExtraLayout);

			ImageView sendContentTipImageView = (ImageView) convertView
					.findViewById(R.id.sendContentTipImageView);
			LinearLayout sendDetailInfoLayout = (LinearLayout) convertView
					.findViewById(R.id.sendDetailInfoLayout);
			TextView sendDetailInfoTipTextView = (TextView) convertView
					.findViewById(R.id.sendDetailInfoTipTextView);


			holder.timeTextView = timeTextView;

			holder.receiveMessageLayout = receiveMessageLayout;
			holder.receiveHeadIconImageView = receiveHeadIconImageView;
			holder.receiveNameTextView = receiveNameTextView;
			holder.receiveContentLayout = receiveContentLayout;
			holder.receiveContentTextView = receiveContentTextView;
			holder.receiveContentImageView = receiveContentImageView;
			holder.receiveContentYuYinLayout = receiveContentYuYinLayout;
			holder.receiveContentYuYinTimeTextView = receiveContentYuYinTimeTextView;
			holder.receiveYuYinExtraLayout = receiveYuYinExtraLayout;
			holder.receiveYuyinReadImageView = receiveYuyinReadImageView;
			holder.receiveChatVoiceImageView = receiveChatVoiceImageView;

			holder.receiveContentProgressBar = receiveContentProgressBar;
			holder.receiveContentTipImageView = receiveContentTipImageView;

			holder.sendMessageLayout = sendMessageLayout;
			holder.sendHeadIconImageView = sendHeadIconImageView;
			holder.sendNameTextView = sendNameTextView;
			holder.sendContentLayout = sendContentLayout;
			holder.sendContentTextView = sendContentTextView;
			holder.sendContentImageView = sendContentImageView;
			holder.sendContentYuYinLayout = sendContentYuYinLayout;
			holder.sendContentYuYinTimeTextView = sendContentYuYinTimeTextView;

			holder.sendContentProgressBar = sendContentProgressBar;
			holder.sendContentReImageView = sendContentReImageView;
			holder.sendYuYinExtraLayout = sendYuYinExtraLayout;
			holder.sendChatVoiceImageView = sendChatVoiceImageView;

			holder.sendContentTipImageView = sendContentTipImageView;
			holder.sendDetailInfoLayout = sendDetailInfoLayout;
			holder.sendDetailInfoTipTextView = sendDetailInfoTipTextView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			resetViewHolder(holder);
		}

		final ChatMessage item = mArrayList.get(position);

		// time show
		if (position > 0) {
			if (DateUtil.isCloseEnough(item.getSendTime(),
					mArrayList.get(position - 1).getSendTime())) {
				holder.timeTextView.setVisibility(View.GONE);
			} else {
				holder.timeTextView.setText(DateUtil.formatChatDateTime(item
						.getSendTime()));
				holder.timeTextView.setVisibility(View.VISIBLE);
			}
		} else {
			holder.timeTextView.setVisibility(View.VISIBLE);
			holder.timeTextView.setText(DateUtil.formatChatDateTime(item
					.getSendTime()));
		}

		// receive
		if (item.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE) {

			// receive
			holder.receiveMessageLayout.setVisibility(View.VISIBLE);
			holder.sendMessageLayout.setVisibility(View.GONE);

			holder.receiveNameTextView.setVisibility(View.GONE);

			if (!TextUtils.isEmpty(item.getGuestProfilePicId())) {
				HeadIconLoadUtil.displayImage(R.drawable.chat_doctor_icon_default,Setting.getImageUrl(item.getGuestProfilePicId()),
						holder.receiveHeadIconImageView);
			}else{
				holder.receiveHeadIconImageView
						.setImageResource(R.drawable.chat_doctor_icon_default);
			}

			holder.receiveContentLayout.setOnClickListener(null);
			holder.receiveContentLayout.setOnLongClickListener(null);

			int messageType = item.getMessageType();
			final Media media = item.getMedia();
			String mediaUrl = "";
			switch (messageType) {
			case 1:// text
				holder.receiveContentTextView.setVisibility(View.VISIBLE);
				holder.receiveContentImageView.setVisibility(View.GONE);
				holder.receiveContentYuYinLayout.setVisibility(View.GONE);
				holder.receiveYuYinExtraLayout.setVisibility(View.GONE);

				// ========================
				// 在线咨询
				if (mType == ChatConst.TYPE_ONLINE_CHAT_ONLINE) {
//					BeanStaffEntity staffEntity = BeanStaffEntity
//							.handleContent(item.getContent());
//					if (staffEntity != null) {
//						holder.receiveContentTextView.setText(staffEntity.Text);
//					} else {
//						holder.receiveContentTextView.setText("");
//					}
					holder.receiveContentTextView.setText(item.getContent());
				} else {
					// 医生聊天 讲座聊天
					holder.receiveContentTextView.setText(item.getContent());
				}
				// ========================

				holder.receiveContentLayout
						.setOnLongClickListener(new OnLongClickListener() {

							@Override
							public boolean onLongClick(View v) {
								String content = holder.receiveContentTextView
										.getText().toString();
								contentTextClick(content);
								return false;
							}
						});

				break;
			case 2:// image
				holder.receiveContentTextView.setVisibility(View.GONE);
				holder.receiveContentImageView.setVisibility(View.VISIBLE);
				holder.receiveContentYuYinLayout.setVisibility(View.GONE);
				holder.receiveYuYinExtraLayout.setVisibility(View.GONE);

//				String filePath = "";
//				if (media != null && media.getFullName() != null) {
//					filePath = media.getFullName();
//				}
//
//				int[] widthHeightArray = ImageUtilBase.getImageWidthHeight(filePath);
//				int srcWidth = widthHeightArray[0];
//				int srcHeight = widthHeightArray[1];
//
//				int newHeight = Const.CHAT_IMAGE_WIDTH_SEND * srcHeight / srcWidth;
//				ViewGroup.LayoutParams paramSendImage = holder.receiveContentImageView.getLayoutParams();
//				handleImageWidth(paramSendImage,Const.CHAT_IMAGE_WIDTH_RECEIVE,newHeight);
//
//				UniversalImageLoadTool.disPlay(
//						ImageUtilBase.getFileImageUrl(filePath),
//						holder.receiveContentImageView,R.drawable.default_icon_small,
//						R.drawable.app_chat_image_download_error);

				String imageUrl = media.getLargeImageId();
				UniversalImageLoadTool.disPlay(imageUrl,
						holder.receiveContentImageView, R.drawable.default_icon_small,
						R.drawable.app_chat_image_download_error);

				String fileImagePath = ImageLoader.getInstance().getDiskCache().get(imageUrl).getPath();
				if(!TextUtils.isEmpty(fileImagePath) && new File(fileImagePath).exists()){
					int[] widthHeightArray = ImageUtilBase.getImageWidthHeight(fileImagePath);
					int srcWidth = widthHeightArray[0];
					int srcHeight = widthHeightArray[1];

					int newHeight = Const.CHAT_IMAGE_WIDTH_SEND * srcHeight / srcWidth;
					ViewGroup.LayoutParams paramReceiveImage = holder.receiveContentImageView.getLayoutParams();
					handleImageWidth(paramReceiveImage, Const.CHAT_IMAGE_WIDTH_SEND, newHeight);
				}

//				String fileImageName = EncodeMD5.getMd5(imageUrl);
//				String fileImagePath = SdLocal.getChatImagePath(mContext,LoginUtil.getUserId(),fileImageName);
//				BitmapManager.getInstance().loadBitmap(imageUrl,fileImagePath,
//						holder.receiveContentImageView,mLoadFailBitmap,true);

				holder.receiveContentLayout
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								contentImageClick(item);
							}
						});

				break;
			case 3:// voice
				holder.receiveContentTextView.setVisibility(View.GONE);
				holder.receiveContentImageView.setVisibility(View.GONE);
				holder.receiveContentYuYinLayout.setVisibility(View.VISIBLE);
				holder.receiveYuYinExtraLayout.setVisibility(View.VISIBLE);

				int duration = item.getMedia().getDuration();
				String time = String.valueOf(duration / 1000 == 0 ? 1
						: duration / 1000) + "''";
				holder.receiveContentYuYinTimeTextView.setText(time);

				LinearLayout.LayoutParams paramReceive = (LinearLayout.LayoutParams) holder.receiveContentYuYinLayout
						.getLayoutParams();
				handleVoiceTextWidth(paramReceive, time);

				if (!item.isRead()) {
					holder.receiveYuyinReadImageView
							.setVisibility(View.VISIBLE);
				} else {
					holder.receiveYuyinReadImageView.setVisibility(View.GONE);
				}
				holder.receiveChatVoiceImageView.setImageResource(R.drawable.chatfrom_voice_playing);

				holder.receiveContentLayout
						.setOnClickListener(new VoicePlayClickListener(item,
								holder.receiveChatVoiceImageView,
								holder.receiveYuyinReadImageView, mContext,position,mListView));

				String downloadUrl = media.getObjectId();
				String fileName = EncodeMD5.getMd5(downloadUrl);
				String filePath = SdLocal.getChatVoicePath(MyApplication.getInstance(), LoginUtil.getUserId(), fileName);

				holder.receiveContentProgressBar.setTag(downloadUrl);
				holder.receiveContentTipImageView.setTag(String.format("%s_tip",downloadUrl));

				File file = new File(filePath);
				if(!file.exists())
				{
					holder.receiveContentProgressBar.setVisibility(View.VISIBLE);
					//download audio file
					downloadFile(downloadUrl);
				}else{
					holder.receiveContentTipImageView.setVisibility(View.GONE);
				}

				break;
			case 4:// video
				break;
			}
		} else {
			// send
			holder.receiveMessageLayout.setVisibility(View.GONE);
			holder.sendMessageLayout.setVisibility(View.VISIBLE);

			holder.sendNameTextView.setVisibility(View.GONE);

			final String userId = LoginUtil.getUserId();
			LoginUtil.loadSelfUserImage(mContext, holder.sendHeadIconImageView, userId, R.drawable.app_ic_default_avatar);

			holder.sendContentLayout.setOnClickListener(null);
			holder.sendContentLayout.setOnLongClickListener(null);

			int messageType = item.getMessageType();
			final Media media = item.getMedia();
			switch (messageType) {
			case 1:// text
				holder.sendContentTextView.setVisibility(View.VISIBLE);
				holder.sendContentImageView.setVisibility(View.GONE);
				holder.sendContentYuYinLayout.setVisibility(View.GONE);
				holder.sendYuYinExtraLayout.setVisibility(View.GONE);

				holder.sendContentTextView.setText(item.getContent());

				holder.sendContentLayout
						.setOnLongClickListener(new OnLongClickListener() {

							@Override
							public boolean onLongClick(View v) {
								String content = holder.sendContentTextView
										.getText().toString();
								contentTextClick(content);
								return false;
							}
						});

				break;
			case 2:// image
				holder.sendContentTextView.setVisibility(View.GONE);
				holder.sendContentImageView.setVisibility(View.VISIBLE);
				holder.sendContentYuYinLayout.setVisibility(View.GONE);
				holder.sendYuYinExtraLayout.setVisibility(View.GONE);

				String filePath = "";
				if (media != null && media.getFullName() != null) {
					filePath = media.getFullName();
				}

				int[] widthHeightArray = ImageUtilBase.getImageWidthHeight(filePath);
				int srcWidth = widthHeightArray[0];
				int srcHeight = widthHeightArray[1];

				int newHeight = Const.CHAT_IMAGE_WIDTH_SEND * srcHeight / srcWidth;
				ViewGroup.LayoutParams paramSendImage = holder.sendContentImageView.getLayoutParams();
				handleImageWidth(paramSendImage,Const.CHAT_IMAGE_WIDTH_SEND,newHeight);

				UniversalImageLoadTool.disPlay(ImageUtilBase.getFileImageUrl(filePath),
						holder.sendContentImageView,R.drawable.default_icon_small,
						R.drawable.app_chat_image_download_error);

				holder.sendContentLayout
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								contentImageClick(item);
							}
						});

				break;
			case 3:// voice
				holder.sendContentTextView.setVisibility(View.GONE);
				holder.sendContentImageView.setVisibility(View.GONE);
				holder.sendContentYuYinLayout.setVisibility(View.VISIBLE);
				holder.sendYuYinExtraLayout.setVisibility(View.VISIBLE);

				int duration = item.getMedia().getDuration();
				String time = String.valueOf(duration / 1000 == 0 ? 1
						: duration / 1000) + "''";
				holder.sendContentYuYinTimeTextView.setText(time);

				LinearLayout.LayoutParams paramReceiveVoice = (LinearLayout.LayoutParams) holder.sendContentYuYinLayout
						.getLayoutParams();
				handleVoiceTextWidth(paramReceiveVoice, time);

				holder.sendChatVoiceImageView.setImageResource(R.drawable.chatto_voice_playing);

				holder.sendContentLayout
						.setOnClickListener(new VoicePlayClickListener(item,
								holder.sendChatVoiceImageView, null, mContext, position, mListView));
				break;
			case 4:// video
				break;
			}

			holder.sendContentReImageView
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							reSendContentCheck(position, item);
						}
					});

			int sendStatus = item.getSendStatus();
			if (sendStatus == Constants.Chat.CHAT_SEND_STATUS_SENT) {
				holder.sendContentProgressBar.setVisibility(View.VISIBLE);
				holder.sendContentReImageView.setVisibility(View.GONE);
			} else if (sendStatus == Constants.Chat.CHAT_SEND_STATUS_LOST
					|| sendStatus == Constants.Chat.CHAT_SEND_STATUS_UPLOAD_FAILED) {
				holder.sendContentProgressBar.setVisibility(View.GONE);
				holder.sendContentReImageView.setVisibility(View.VISIBLE);
			}else {
				holder.sendContentProgressBar.setVisibility(View.GONE);
				holder.sendContentReImageView.setVisibility(View.GONE);
			}

			if(sendStatus == Constants.Chat.CHAT_SEND_STATUS_USER_FORBIDEN){
				//被禁言
				holder.sendContentTipImageView.setVisibility(View.VISIBLE);
				holder.sendDetailInfoLayout.setVisibility(View.VISIBLE);
				holder.sendDetailInfoTipTextView.setText(R.string.chat_askdoctor_block_tip);
			}else{
				holder.sendContentTipImageView.setVisibility(View.GONE);
				holder.sendDetailInfoLayout.setVisibility(View.GONE);
			}
		}

		return convertView;
	}

	private void downloadFile(final String ServerUrl) {
		final String fileName = EncodeMD5.getMd5(ServerUrl);
		final String ServerPath = SdLocal.getChatVoicePath(mContext, LoginUtil.getUserId(), fileName);

		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpRequest.downloadFileBy(ServerUrl, ServerPath, new CallBack() {
					@Override
					public void callBackSuccess(Object obj) {
						super.callBackSuccess(obj);

						Message msg = mHandler.obtainMessage();
						msg.what = 10;
						msg.obj = ServerUrl;
						mHandler.sendMessage(msg);
					}

					@Override
					public void callBackFail(Object obj) {
						super.callBackFail(obj);
						Message msg = mHandler.obtainMessage();
						msg.what = 11;
						msg.obj = ServerUrl;
						mHandler.sendMessage(msg);
					}
				});
			}
		}).start();
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 10:
				case 11:

					String url = (String) msg.obj;

					ProgressBar progressBar = (ProgressBar) mListView.findViewWithTag(String.format("%s", url));
					if (progressBar != null) {
						progressBar.setVisibility(View.GONE);
					}

					if(msg.what == 11){
						ImageView receiveContentTipImageView = (ImageView) mListView.findViewWithTag(String.format("%s_tip", url));
						if (receiveContentTipImageView != null) {
							receiveContentTipImageView.setVisibility(View.VISIBLE);
						}
					}

					break;

				default:
					break;
			}
		}
	};

	@SuppressWarnings("deprecation")
	@SuppressLint("NewApi")
	private void contentTextClick(String content) {
		ManagerClipborad.getInstance().getClipboardM(mContext).setText(content);
		UIHelper.showToast(mContext, "拷贝成功");
	}

	/**
	 * 图片点击
	 * 
	 * @param message
	 */
	private void contentImageClick(ChatMessage message) {
		String[] urlArray = new String[mImageDataContainer.size()];
		int index = 0;

		String mediaUrl = "";
		if(message.getDirection() == Constants.Chat.CHAT_DIRECTION_RECEIVE){
			mediaUrl = message.getMedia().getLargeImageId();
		}else{
			mediaUrl = message.getMedia().getFullName();
		}

		for (int i=0;i<mImageDataContainer.size();i++) {
			String value = mImageDataContainer.get(i);
			if (value.equals(mediaUrl)) {
				index = i;
			}
			urlArray[i] = value;
		}

		Intent intent = new Intent(mContext, AppImageBroswerActivity.class);
		intent.putExtra("isShowTopbar", false);
		intent.putExtra("index", index);
		intent.putExtra("urlArray", urlArray);
		((BaseFragmentActivity) mContext).startActivity(intent);
	}

	private void handleImageWidth(ViewGroup.LayoutParams params,int width,int height)
	{
		params.width = width;
		if(height > 0){
			params.height = height;
		}
	}

	private void handleVoiceTextWidth(LinearLayout.LayoutParams params,
			String time) {
		int duration = Integer.valueOf(time.split("''")[0]);

		if ((time.split("''")[0]).contains("'")) {
			params.width = 350;
			params.gravity = Gravity.RIGHT;
			return;
		}
		if (duration < 3) {
			params.width = 130;
		} else if (duration == 3) {
			params.width = 150;
		} else if (duration == 4) {
			params.width = 170;
		} else if (duration == 5) {
			params.width = 180;
		} else if (duration == 6) {
			params.width = 200;
		} else if (duration == 7) {
			params.width = 220;
		} else if (duration == 8) {
			params.width = 240;
		} else if (duration == 9) {
			params.width = 260;
		} else if (duration > 9 && duration < 20) {
			params.width = 280;
		} else if (duration > 19 && duration < 30) {
			params.width = 250;
		} else if (duration > 29 && duration < 40) {
			params.width = 300;
		} else if (duration > 39 && duration < 50) {
			params.width = 290;
		} else if (duration > 49 && duration < 60) {
			params.width = 320;
		} else if (duration >= 60) {
			params.width = 350;
		}
		params.gravity = Gravity.RIGHT;
	}

	private void reSendContentCheck(final int position, ChatMessage chatMessage) {
		String message = "您确定要重发这条消息吗？";
		Activity currentActivity = (Activity) mContext;
		MyDialog dialog = new MyDialog(currentActivity)
				.setMessage(message)
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						reSendContent(position);
					}
				});

		dialog.create(null);
		dialog.showMyDialog();
	}

	private void reSendContent(int position) {
		if (mChatSendListener != null) {
			mChatSendListener.reSendContent(position);
		}
	}

	private void resetViewHolder(ViewHolder holder)
	{
		holder.timeTextView.setText("");

		// receive
		holder.receiveHeadIconImageView.setImageResource(0);
		holder.receiveNameTextView.setText("");// ?????
		holder.receiveContentTextView.setText("");
		holder.receiveContentYuYinTimeTextView.setText("");

		holder.receiveContentLayout.setTag("");

		holder.receiveContentImageView.setImageResource(0);
		holder.receiveChatVoiceImageView.setImageResource(0);

		// send
		holder.sendHeadIconImageView.setImageResource(0);
		holder.sendNameTextView.setText("");// ?????
		holder.sendContentTextView.setText("");
		holder.sendContentYuYinTimeTextView.setText("");
		holder.sendDetailInfoTipTextView.setText("");

		holder.sendContentImageView.setImageResource(0);
		holder.sendChatVoiceImageView.setImageResource(0);
	}


	private static class ViewHolder {
		TextView timeTextView;

		// receive
		RelativeLayout receiveMessageLayout;
		CircularImageView receiveHeadIconImageView;
		TextView receiveNameTextView;// ?????
		LinearLayout receiveContentLayout;
		TextView receiveContentTextView;
		ImageView receiveContentImageView;
		LinearLayout receiveContentYuYinLayout;

		LinearLayout receiveYuYinExtraLayout;
		ImageView receiveChatVoiceImageView;
		ImageView receiveYuyinReadImageView;
		TextView receiveContentYuYinTimeTextView;

		ProgressBar receiveContentProgressBar;
		ImageView receiveContentTipImageView;

		// send
		RelativeLayout sendMessageLayout;
		CircularImageView sendHeadIconImageView;
		TextView sendNameTextView;// ?????
		LinearLayout sendContentLayout;
		TextView sendContentTextView;
		ImageView sendContentImageView;
		LinearLayout sendContentYuYinLayout;

		ProgressBar sendContentProgressBar;
		ImageView sendContentReImageView;

		LinearLayout sendYuYinExtraLayout;
		ImageView sendChatVoiceImageView;
		TextView sendContentYuYinTimeTextView;

		ImageView sendContentTipImageView;
		LinearLayout sendDetailInfoLayout;
		TextView sendDetailInfoTipTextView;
	}
}
